cc.Class({
    extends: cc.Component,

    properties: {
        timeTxt:cc.Label,
        countDownTxt:cc.Label,
        successPop:cc.Node,
        successTime:cc.Label,
        failPop:cc.Node,
    },

    // use this for initialization
    onLoad: function () {
        cc.director.getPhysicsManager().enabled = true;
        //cc.director.getCollisionManager().enabled = true;
        //cc.director.getCollisionManager().enabledDebugDraw = true;
        //cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_shapeBit;
        this.initGame();
        this.countDown();
    },

    timeout:function() {
        this.time++;
        this.timeTxt.string = formatTime(this.time);
    },

    gameStart:function() {
        this.schedule(this.timeout, 1);
    },

    gameOver:function() {
        this.unschedule(this.timeout);
    },

    initGame:function() {
        this.time = 0;
    },

    refreshGame:function() {
        this.time = 0;
    },

    countDown:function() {
        this.countDownTxt.node.parent.active = true;
        let num  = 3;
        this.countDownTxt.node.runAction(cc.sequence(cc.repeat(cc.sequence(cc.scaleTo(0.5,1),cc.scaleTo(0.5,0)),3),cc.callFunc(function(){
            this.countDownTxt.node.parent.active = false;
            this.gameStart();
        },this)));
        this.schedule(function(){
            num--;
            this.countDownTxt.string = num;
        }, 1,2);
    },

    fail:function() {
        this.gameOver();
        this.failPop.active = true;
    },

    success:function() {
        this.gameOver();
        this.successTime.string = formatTime(this.time);
        this.successPop.active = true;
    },

    goDraw:function() {
        window.drawFlag = true;
        cc.director.loadScene("start");
    },

    goLevel:function() {
        window.guideFlag = true;
        cc.director.loadScene("start");
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});

function formatTime(time) {  
    let min = parseInt(time/60);
    let n = '00';
    let s = (time%60).toString();
    let sec = n.substring(0, n.length - s.length) + s;
    return min+'"'+sec;
}
